package com.mqy.config;


import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;

/**
 * 通过 BasicDataSource 创建 JdbcTemplate
 */
@Configuration
public class PhoenixConnectionByBasicDataSource {

    @Value("ZK_ADDRESS_DEBUG")
    private String ZK_ADDRESS_DEBUG;

    public BasicDataSource phoenixDataSource() {
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setDriverClassName("org.apache.phoenix.jdbc.PhoenixDriver");
        //http://tech.dianwoda.com/2017/08/08/di-fang-wen-liang-shi-phoenix-4-10-1-bao-cuo-connection-is-null-or-closed/
        //phoenix不推荐使用连接池
        dataSource.setInitialSize(0);
        dataSource.setMinIdle(0);
        dataSource.setMaxActive(50);
        dataSource.setMaxWait(5000);
        dataSource.setTimeBetweenEvictionRunsMillis(1000);
        dataSource.setMinEvictableIdleTimeMillis(5000);
        dataSource.setUrl(ZK_ADDRESS_DEBUG);
        return dataSource;
    }

    @Bean
    public JdbcTemplate phoenixJdbcTemplate() {
        JdbcTemplate jdbcTemplate = new JdbcTemplate();
        jdbcTemplate.setDataSource(phoenixDataSource());
        return jdbcTemplate;
    }

}
